import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login/Login.vue'
import Reg from '../views/Reg/Reg.vue'
import Main from '../views/Main/Main.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/',
    component: Main
  }
]

const router = new VueRouter({
  routes
})
// 设置路由前置守卫
// 如果token不对，且要去的页面不是登录和注册，就强行跳转到登录页
router.beforeEach((to, from, next) => {
  // if(!store.state.user.token && to.path !=='/login' && to.parh !=='/reg')
  const path = ['/login', '/user']
  if (!store.state.user.token && !path.includes(to.path)) {
    Vue.prototype.$message.error('你没登录啊')
    return next('/login')
  }
})
export default router
